package modelLayer;

import java.util.ArrayList;
import java.util.List;

public class ScheduleCon {
	private static ScheduleCon instance;
	private ArrayList<Schedule> scheduleList;
	
	private ScheduleCon(){
		scheduleList = new ArrayList<>();
	}
	
	public static ScheduleCon getInstance(){
		if (instance == null){
			instance = new ScheduleCon();
		}
		return instance;
	}
	
	public void addSchedule(Schedule s){
		scheduleList.add(s);
	}
	
	public void removeSchedule(Schedule s){
		boolean temp = false;
		for (int i = 0; i < scheduleList.size() && temp == false; i++){
			if (scheduleList.get(i).equals(s)){
				temp = scheduleList.remove(scheduleList.get(i));
			}
		}
	}
	
	public Schedule findSchedule(String name){
		boolean temp = false;
		Schedule s = null; 
		for (int i = 0; i < scheduleList.size() && !temp; ++i){
			if (scheduleList.get(i).getCl().getClassName().equalsIgnoreCase(name)){
				s = scheduleList.get(i);
				temp = true;
			}
		}
		return s;
	}
	
	public List<Schedule> getAll(){
		List<Schedule> all = new ArrayList<>();
		all = scheduleList;
		return all;
	}
	
	/**
	 * Returns those Block objects whose blockNo equals the parameter blockNo.
	 * @param blockNo
	 * @return
	 */
	public List<Block> getBlocksByNumber(int blockNo){
		List<Block> blocks = new ArrayList<>();
		
		for(Schedule sch : scheduleList){
			List<Block> blocksOfSch = sch.getBlocks();
			for (Block b : blocksOfSch){
				if (b.getTeachingBlock() != null && b.getNumber() == blockNo){
					blocks.add(b);
				}
			}
		}
		return blocks;
	}
	
	
	/**
	 * Returns all existing Blocks form all Schedules.
	 * @return
	 */
	public List<Block> getAllBlocks(){
		List<Block> blocks = new ArrayList<>();
		
		for(Schedule sch : scheduleList){
			List<Block> blocksOfSch = sch.getBlocks();
			for (Block b : blocksOfSch){
				blocks.add(b);
			}
		}
		return blocks;
	}
	
	
}
